package ca.concordia.soen6461.wichf.navigation.view;

import static ca.concordia.soen6461.wichf.navigation.view.HomeView.getDate;

import java.util.Date;

import ca.concordia.soen6461.wichf.controller.Event;
import ca.concordia.soen6461.wichf.entity.ScheduledEvent;
import ca.concordia.soen6461.wichf.entity.User;
import ca.concordia.soen6461.wichf.navigation.event.CancelAppointmentEvent;
import ca.concordia.soen6461.wichf.navigation.event.MakeCheckupAppointmentEvent;
import ca.concordia.soen6461.wichf.navigation.event.MakeVisitAppointmentEvent;
import ca.concordia.soen6461.wichf.navigation.view.HomeViewBean.TimeSlot;
import ca.concordia.soen6461.wichf.service.SchedulingService;

public class PatientHomeView extends BaseHomeView {

	private SchedulingService schedulingService;
	
	public PatientHomeView(SchedulingService schedulingService) {
		this.schedulingService = schedulingService;
	}

	@Override
	protected void populateSlot(Event event, User user, int d, int t, TimeSlot slot) {
		Date time = getDate(d, t).getTime();
		Date time1 = getDate(d, t + 1).getTime();
		Date time2 = getDate(d, t + 2).getTime();
		SchedulingService.ClinicStatus clinicStatus = schedulingService.clinicStatus(user, time);
		SchedulingService.ClinicStatus clinicStatus1 = schedulingService.clinicStatus(user, time1);
		SchedulingService.ClinicStatus clinicStatus2 = schedulingService.clinicStatus(user, time2);
		ScheduledEvent userAppointment = clinicStatus.getAppointment();
		if (userAppointment != null) {
			slot.setAppointment(true);
			if (time.equals(userAppointment.getFrom()) && !userAppointment.isConfirmed()) {
				slot.getActions().put(CancelAppointmentEvent.class.getSimpleName(), "Cancel");
			}
		} else if (clinicStatus.hasAvailability()) {
			slot.getActions().put(MakeVisitAppointmentEvent.class.getSimpleName(), "Visit");
			if (!clinicStatus.hasExhaustedCheckups() && clinicStatus1.hasAvailability() && clinicStatus2.hasAvailability()) {
				slot.getActions().put(MakeCheckupAppointmentEvent.class.getSimpleName(), "Checkup");
			}
		}
	}

}
